package com.sanpu.kxe.business.card.model;

import com.alibaba.fastjson.JSONObject;
import com.sanpu.kxe.business.card.bean.BankInfoInput;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.bean.CardOutput;
import com.sanpu.kxe.business.card.bean.DefBankOcrOutput;
import com.sanpu.kxe.business.card.bean.DefBankOutput;
import com.sanpu.kxe.rxservice.DataUtils;
import com.sanpu.kxe.rxservice.client.BankClient;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

import rx.Observable;

/**
 * Description：银行卡模块model
 * Created by 薛勇军 on 2018/7/16.
 */

public class CardModel {

    private static volatile CardModel mInstance;

    private CardModel() {
        if (mInstance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    public static CardModel getInstance() {
        CardModel result = mInstance;
        if (result == null) {
            synchronized (CardModel.class) {
                result = mInstance;
                if (result == null) {
                    mInstance = result = new CardModel();
                }
            }
        }
        return result;
    }

    /**
     * 添加银行卡
     */
    public Observable<String> insertUrmBank(BankInfoInput input) {
        JSONObject reqData = new JSONObject();
        reqData.put("bankNo", input.getBankNo());
        reqData.put("rsdTel", input.getRsdTel());
//        reqData.put("cardType", input.getCardType());
//        reqData.put("bankCode", input.getBankCode());
//        reqData.put("phoneNo", input.getRsdTel());
//        try {
//            reqData.put("crpNm", URLEncoder.encode(input.getCrpNm(), "UTF-8"));
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        reqData.put("smsCode", input.getSmsCode());
        reqData.put("billDay", input.getBillDay());
        reqData.put("dueDay", input.getDueDay());
        return BankClient.getInstance().insertUrmBank(reqData);
    }

    /**
     * 添加信用卡
     */
    public Observable<String> insertCreditBank(String bankNo, String rsdTel, String billDay, String dueDay) {
        JSONObject reqData = new JSONObject();
        reqData.put("bankNo", bankNo);
        reqData.put("rsdTel", rsdTel);
        reqData.put("billDay", billDay);
        reqData.put("dueDay", dueDay);
        return BankClient.getInstance().insertCreditBank(reqData);
    }

    /**
     * 我的银行卡列表
     * @param cardType 00借记卡 01信用卡 空为全部
     */
    public Observable<List<BankInfoOutput>> selectUrmBankPage(String cardType) {
        JSONObject reqData = new JSONObject();
        reqData.put("cardType", cardType);
        return BankClient.getInstance().selectUrmBankPage(reqData);
    }

    /**
     * 获取结算卡审核状态
     */
    public Observable<DefBankOutput> getDefBankStatus() {
        return BankClient.getInstance().getDefBankStatus();
    }

    /**
     * 查询银行卡信息
     * @param bankNo  银行卡号码
     * @return
     */
    public Observable<CardOutput> bankInfo(String bankNo, boolean allBank, boolean addCard) {
        JSONObject reqData = new JSONObject();
        reqData.put("bankNo", bankNo);
        reqData.put("allBank", String.valueOf(allBank));
        reqData.put("addCard", String.valueOf(addCard));
        return BankClient.getInstance().bankInfo(reqData);
    }

    /**
     * 修改银行卡预留手机号
     * @param merchantCode 商户号
     * @param bankNo       银行卡号码
     * @param rsdTel       预留手机号
     * @return
     */
    public Observable<String> updateBankRsdTel(String merchantCode, String bankNo, String rsdTel) {
        JSONObject reqData = new JSONObject();
        reqData.put("merchantCode", merchantCode);
        reqData.put("bankNo", bankNo);
        reqData.put("rsdTel", rsdTel);
        return BankClient.getInstance().updateBankRsdTel(reqData);
    }

    /**
     * 修改默认结算卡
     * @param bankNo   银行卡号
     * @param rsdTel   预留手机号
     * @param imgFiles 证件照集合
     * @return
     */
    public Observable<String> updateDefUrmBank(String bankNo, String rsdTel, HashMap<String, File> imgFiles) {
        JSONObject reqData = new JSONObject();
        reqData.put("bankNo", bankNo);
        reqData.put("rsdTel", rsdTel);
        return BankClient.getInstance().updateDefUrmBank(reqData, imgFiles);
    }

    /**
     * 修改默认结算卡(银行卡识别和人脸对比)
     * @param imgFiles 证件照集合
     * @return
     */
    public Observable<DefBankOcrOutput> ocrBankCardAndFaceMatch(HashMap<String, File> imgFiles) {
        return BankClient.getInstance().ocrBankCardAndFaceMatch(imgFiles);
    }

    /**
     * 修改默认结算卡（鉴权）
     * @return
     */
    public Observable<String> modifyDefUrmBank(DefBankOcrOutput ocrInput) {
        String bankName = null;
        try {
            bankName = URLEncoder.encode(ocrInput.getBankName(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JSONObject reqData = JSONObject.parseObject(JSONObject.toJSON(ocrInput).toString());
        reqData.put("bankName", bankName);
        return BankClient.getInstance().modifyDefUrmBank(reqData);
    }

    /**
     * 修改默认结算卡（无需照片和审核）
     * @return
     */
    public Observable<String> modifyDefaultCard(String cardNo, String reservePhoneNumber) {
        JSONObject reqData = new JSONObject();
        reqData.put("cardNo", cardNo);
        reqData.put("reservePhoneNumber", reservePhoneNumber);
        return BankClient.getInstance().modifyDefaultCard(reqData);
    }

    /**
     * 银行卡解绑
     * @param bankNo 银行卡号码
     * @return
     */
    public Observable<String> deleteUrmBank(String bankNo) {
        JSONObject reqData = new JSONObject();
        reqData.put("bankNo", bankNo);
        return BankClient.getInstance().deleteUrmBank(reqData);
    }

    /**
     * 提现
     * @param accountName 账户
     * @param withdrawAmt 提现金额
     * @param payPassword 支付密码
     * @param bankId      银行卡id
     * @return
     */
    public Observable<String> userWithdraw(String accountName, String withdrawAmt,
                                           String payPassword, String bankId) {
        JSONObject reqData = new JSONObject();
        reqData.put("withdrawAmt", withdrawAmt);
        try {
            reqData.put("payPassword", DataUtils.getMD5Str(payPassword+accountName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        reqData.put("bankId", bankId);
        return BankClient.getInstance().userWithdraw(reqData);
    }
}
